package Z_DeadLine;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import utils.RabbitMQUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.TimeoutException;

/**
 * @author： pang-yun
 * @date： 2021-12-21 13:30
 * 功能目的 ：用于
 */

public class DeadConsumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //拿到信道
        Channel channel = RabbitMQUtils.getChannel();
        //声明普通 死信 交换机  类型为 direct
        channel.exchangeDeclare(DeadLineConst.NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);
        channel.exchangeDeclare(DeadLineConst.DEAD_EXCHANGE, BuiltinExchangeType.DIRECT);

        //------------------------------------------------------
        HashMap<String, Object> arguments = new HashMap<>();
        // 正常队列设置死信交换机
        arguments.put(DeadLineConst.DEAD_LETTER, DeadLineConst.DEAD_EXCHANGE);
        //设置死信队列 RouteKey
        arguments.put(DeadLineConst.DEAD_ROUTE, DeadLineConst.ROUTE_KEY);
        //设置正常队列的最大长度
        arguments.put("x-max-length", 6);

        // 声明 普通队列
        channel.queueDeclare(DeadLineConst.NORMAL_QUEUE, false, false, false, arguments);
        //-----------------------------------------------------------------
        //死信队列
        channel.queueDeclare(DeadLineConst.DEAD_QUEUE, false, false, false, null);


        //--------------将交换机 与队列绑定-----------------
        channel.queueBind(DeadLineConst.NORMAL_QUEUE, DeadLineConst.NORMAL_EXCHANGE, DeadLineConst.NORMAL_ROUTE_KEY);
        channel.queueBind(DeadLineConst.DEAD_QUEUE, DeadLineConst.DEAD_EXCHANGE, DeadLineConst.ROUTE_KEY);

        //-------------消费消息
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            System.out.println("consumer-01 :" + new String(message.getBody()));
        };
        channel.basicConsume(DeadLineConst.NORMAL_QUEUE,true, deliverCallback, (consumerTag -> {
            System.out.println("failed");
        }));
    }
}
